from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

class Movie(models.Model):
    title = models.CharField(max_length=200)
    director = models.CharField(max_length=100)
    release_year = models.PositiveIntegerField(
        validators=[
            MinValueValidator(1900),
            MaxValueValidator(2100)
        ]
    )
    genre = models.CharField(max_length=100)
    description = models.TextField()
    rating = models.FloatField()

    class Meta:
        ordering = ['-release_year']

    def __str__(self):
        return self.title

class MovieImage(models.Model):
    movie = models.ForeignKey(Movie, related_name='images', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='movies/')
    caption = models.CharField(max_length=200, blank=True)
    
    def __str__(self):
        return f"Image for {self.movie.title}"

class MovieLink(models.Model):
    LINK_TYPES = [
        ('stream', 'Streaming'),
        ('download', 'Download'),
        ('info', 'Information'),
        ('social', 'Social Media'),
    ]
    
    movie = models.ForeignKey(Movie, related_name='links', on_delete=models.CASCADE)
    link_type = models.CharField(max_length=20, choices=LINK_TYPES)
    name = models.CharField(max_length=100)
    url = models.URLField()
    description = models.TextField(blank=True)
    
    def __str__(self):
        return f"{self.name} for {self.movie.title}"
